package com.lanou3g.musicenjoy.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou3g.musicenjoy.R;
import com.lanou3g.musicenjoy.adapter.RecomNewAdapter;
import com.lanou3g.musicenjoy.entity.NewGride;
import com.lanou3g.musicenjoy.volley.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 15/10/27.
 */
public class MoreFragment extends Fragment implements View.OnClickListener, AdapterView.OnItemClickListener {
    private RecomNewAdapter newAdapter;
    private List<NewGride.PlazeAlbumListEntity.RMEntity.AlbumListEntity.ListEntity> dataN;
    private View view;
    private ImageView moreImageView;
    private GridView moreGrideView;
    private RequestQueue requestQueue;
    private MoreHelperFragment leftFragment;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_more,null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        requestQueue= VolleySingleton.getInstance(getActivity()).getRequestQueue();
        moreImageView= (ImageView) view.findViewById(R.id.moreImageView);
        moreImageView.setOnClickListener(this);
        moreGrideView= (GridView) view.findViewById(R.id.moreGridView);
        moreGrideView.setOnItemClickListener(this);
        newAdapter=new RecomNewAdapter(getActivity());
        moreGrideView.setAdapter(newAdapter);
        String url="http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0&method=baidu.ting.plaza.getRecommendAlbum&format=json&offset=0&limit=50";
        StringRequest request=new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (response!=null){
                    Gson gson=new Gson();
                    NewGride result=gson.fromJson(response,NewGride.class);
                    if (result!=null){
                        dataN=result.getPlaze_album_list().getRM().getAlbum_list().getList();
                        newAdapter.initData(dataN);
                    }
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        requestQueue.add(request);
    }

    @Override
    public void onClick(View v) {
        getFragmentManager().popBackStack();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        leftFragment=new MoreHelperFragment();
      getFragmentManager()
              .beginTransaction()
              .addToBackStack(null)
              .replace(R.id.container, leftFragment)
              .commit();
        Bundle bundle=new Bundle();
        String index=dataN.get(position).getAlbum_id();
        String url=dataN.get(position).getPic_radio();
        bundle.putString("id",index);
        bundle.putString("url",url);
        leftFragment.setArguments(bundle);
    }
}
